//
//  AppsLocationManager.m
//  Fineland
//
//  Created by Rayco on 12-12-13.
//  Copyright (c) 2012年 Apps123. All rights reserved.
//

#import "AppsLocationManager.h"
#import "WeatherGetter.h"

@interface AppsLocationManager()<CLLocationManagerDelegate>/*<BMKMapViewDelegate>*/ {
//    BMKMapManager *_mapManager;
//    BMKMapView* _mapView;
}

@end

static AppsLocationManager *defaultManager;
@implementation AppsLocationManager
@synthesize currentLocation = _currentLocation;
@synthesize lastTimeLocation = _lastTimeLocation;

#pragma mark - Public

+ (id)sharedManager {
    @synchronized(@"AppsLocationManager"){
        if(!defaultManager){
            defaultManager = [[self alloc] init];
        }
    }
    return defaultManager;
}

- (void)startUpdate {
    [_locationManager startUpdatingLocation];
//    _mapView = [[BMKMapView alloc] init];//WithFrame:CGRectMake(0,0,320,320)
//    [_mapView setDelegate:self];
//    _mapView.showsUserLocation = YES;
}

- (void)stopUpdate {
//    _mapView.userTrackingMode = BMKUserTrackingModeNone;
//    _mapView.showsUserLocation = NO;
}
//
- (void)destoyMapView {
//    if (_mapView.showsUserLocation) {
//        [self stopUpdate];
//    }
//    _mapView.delegate = nil;
//    _mapView = nil;
//    defaultManager = nil;
}


- (double)getDistance:(CLLocation*)_location {
    return [_currentLocation distanceFromLocation:_location];
    
}

//#pragma mark - BMKMapViewDelegate
///**
// *用户位置更新后，会调用此函数
// *@param mapView 地图View
// *@param userLocation 新的用户位置
// */
//- (void)mapView:(BMKMapView *)mapView didUpdateUserLocation:(BMKUserLocation *)userLocation {
//	if (userLocation != nil) {
//        _lastTimeLocation = _currentLocation;
//        
//        if (abs(_currentLocation.location.coordinate.latitude - userLocation.location.coordinate.latitude) >= 1 || abs(_currentLocation.location.coordinate.longitude - userLocation.location.coordinate.longitude) >= 1) {
//            //打开程序,第一次更新
//            _currentLocation = userLocation;
//            
//            [[NSNotificationCenter defaultCenter] postNotificationName:Notification_Location_Updated object:nil];
//        }
//        _currentLocation = userLocation;
//        
//	}
//	
//}
//
///**
// *定位失败后，会调用此函数
// *@param mapView 地图View
// *@param error 错误号，参考CLError.h中定义的错误号
// */
//- (void)mapView:(BMKMapView *)mapView didFailToLocateUserWithError:(NSError *)error {
//    NSLog(@"获取当前位置失败");
//    _currentLocation = nil;
//    _lastTimeLocation = nil;
//}
//
///**
// *在地图View将要启动定位时，会调用此函数
// *@param mapView 地图View
// */
//- (void)mapViewWillStartLocatingUser:(BMKMapView *)mapView {
//    
//}
//
///**
// *在地图View停止定位后，会调用此函数
// *@param mapView 地图View
// */
//- (void)mapViewDidStopLocatingUser:(BMKMapView *)mapView {
//    
//}
#pragma mark - CLLocationManagerDelgate

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    _lastTimeLocation = _currentLocation;
    _currentLocation = newLocation;
    [[NSNotificationCenter defaultCenter] postNotificationName:Notification_Location_Updated object:nil];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
    //自带地图返回的是真实的经纬度
    if (!locations || [locations count] < 1) {
        return;
    }
    
    if (!_currentLocation || [_currentLocation distanceFromLocation:[locations lastObject]] > 100) {
        _currentLocation = [locations lastObject];
        [[NSNotificationCenter defaultCenter] postNotificationName:Notification_Location_Updated object:nil];
    }
    
    //将真实经纬度转化为百度的经纬度 -- 需要百度sdk支持
//    NSDictionary *dict = BMKBaiduCoorForWgs84(originLocation.coordinate);
//    CLLocationCoordinate2D BD09Coordinate = BMKCoorDictionaryDecode(dict);
//    _currentLocation = [[CLLocation alloc] initWithLatitude:BD09Coordinate.latitude longitude:BD09Coordinate.longitude];
}

- (void)locationManager:(CLLocationManager *)manager monitoringDidFailForRegion:(CLRegion *)region withError:(NSError *)error{
    NSLog(@"%@",error);
}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    NSLog(@"%@",error);
}

//ios8的新方法
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
    switch (status) {
        case kCLAuthorizationStatusNotDetermined:
            if ([_locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
                [_locationManager performSelector:@selector(requestAlwaysAuthorization)];
            }
            break;
        default:
            break;
    }
}


#pragma mark - lifecycle

- (id)init {
    self = [super init];
    if (self) {
        if (!_locationManager) {
            _locationManager = [[CLLocationManager alloc] init];
        }
        
        //ios8的新方法
        if ([_locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
            [_locationManager performSelector:@selector(requestAlwaysAuthorization)];
        }
        if([CLLocationManager locationServicesEnabled]){
            _locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
            _locationManager.delegate = self;
        }
    }
    return self;
}

@end
